Component({
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    startYear: {
      type: String,
      observer: function (oldVal, newVal) {
        this.setData({
          kanDate: oldVal
        })

      }
    }

  },
  data: {
    // 这里是一些组件内部数据
    kanDate: '',
    currentDate: '',
    dayList: '',
    currentDayList: '',
    currentObj: '',
    currentDay: '',
    newCurrentDayList: '',
    currentM: '',
    kanNum: ''
  },
  attached: function (options) {
    var currentObj = this.getCurrentDayString();
    this.setData({
      currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
      currentDay: currentObj.getDate(),
      currentObj: currentObj,
      currentM: (currentObj.getMonth() + 1)
    });
    this.setSchedule(currentObj);

  },
  methods: {
    // 这里是一个自定义方法
    doDay: function (e) {
      var that = this;
      var currentObj = that.data.currentObj;
      var Y = currentObj.getFullYear();
      var m = currentObj.getMonth() + 1;
      var d = currentObj.getDate();
      var str = ''
      if (e.currentTarget.dataset.key == 'left') {
        m -= 1
        if (m <= 0) {
          str = (Y - 1) + '/' + 12 + '/' + d
        } else {
          str = Y + '/' + m + '/' + d
        }
      } else {
        m += 1
        if (m <= 12) {
          str = Y + '/' + m + '/' + d
        } else {
          str = (Y + 1) + '/' + 1 + '/' + d
        }
      }
      currentObj = new Date(str)
      this.setData({
        currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
        currentObj: currentObj
      })
      this.setSchedule(currentObj);
    },
    getCurrentDayString: function () {
      let that = this;
      var objDate = this.data.currentObj
      if (objDate != '') {
        return objDate
      } else {
        var c_obj = new Date();
        let startM = that.getDateArr(that.data.kanDate)[0];
        startM = startM.split("-")[1];
        c_obj.setMonth(Number(startM)-1);
        var a = c_obj.getFullYear() + '/' + (c_obj.getMonth() + 1) + '/' + c_obj.getDate()
        return new Date(a);
      }
    },
    setSchedule: function (currentObj) {
      var that = this
      var m = currentObj.getMonth() + 1
      var Y = currentObj.getFullYear()
      var d = currentObj.getDate();
      var dayString = Y + '/' + m + '/' + currentObj.getDate()
      var currentDayNum = new Date(Y, m, 0).getDate()
      var currentDayWeek = currentObj.getUTCDay() + 1
      var result = currentDayWeek - (d % 7 - 1);
      var firstKey = result <= 0 ? 7 + result : result;
      var currentDayList = [];
      var newCurrentDayList = [];
      var f = 0;
      //加月
      that.setData({
        currentM: m
      })
      for (var i = 0; i < 42; i++) {
        let data = []
        if (i < firstKey - 1) {
          currentDayList[i] = ''
        } else {
          if (f < currentDayNum) {
            currentDayList[i] = f + 1
            f = currentDayList[i]
          } else if (f >= currentDayNum) {
            currentDayList[i] = ''
          }
        }
      }
      that.setData({
        currentDayList: currentDayList
      });
      
      that.data.currentDayList.forEach(function (val, index) {
        if (val != "") {
          newCurrentDayList.push(val);
        } else {
          newCurrentDayList.push("")
        }
      })
      newCurrentDayList.forEach(function (val, index) {
        if (val != "") {
          newCurrentDayList[index] = Y + '-' + that.numSet(that.data.currentM) + '-' + that.numSet(val)
        } else {
          newCurrentDayList[index] = ""
        }
      })
      let join = that.getNum(that.getDateArr(that.data.kanDate), newCurrentDayList);
      that.setData({
        kanNum: join
      })
    },
    //处理时间日期
    getDateArr: function (date) {
      if (date.indexOf(',') == -1) {
        let arr = [];
        arr.push(date);
        return arr
      } else {
        let arr = date.split(',');
        return arr
      }
       
    },
    //匹配处理
    getNum: function (target, date) {
      var join = {};
      target.forEach(function (val1) {
        date.forEach(function (val2, index) {
          if (val1 == val2) {
            if (!join[index]) {
              join[index] = index
            } else {
              return
            }
          }
        })
      })
      return join
    },
    //日期加0处理
    numSet: function(num){
      if (Number(num) >= 10){
        return '' + Number(num)
      }else{
        return '0' + Number(num)
      }
    }
  }

})